package net.sashag.nadlandroid;

import org.acra.ACRA;
import org.acra.annotation.ReportsCrashes;

import android.app.Application;
import android.util.Log;

import com.parse.Parse;
import com.parse.ParseACL;
import com.parse.ParseAnonymousUtils;
import com.parse.ParseFacebookUtils;
import com.parse.ParseUser;

@ReportsCrashes(formKey = "dFYxR0sxSWhEMElTVUMtaVlqZk5ZWUE6MQ")
public class NadlandroidApplication extends Application {

	@Override
	public void onCreate() {
		super.onCreate();
		
		Log.i("Nadlandroid", "Running debug build? " + DebugBuildHelper.isRunningDebugBuild(this));
		
		ACRA.init(this);
		
		String parseAppId = getString(R.string.parseAppId);
		String parseApiKey = getString(R.string.parseApiKey);
		Parse.initialize(this, parseAppId, parseApiKey);
		
		String facebookAppId = getString(R.string.facebookAppId);
		ParseFacebookUtils.initialize(facebookAppId, true /*enable single sign on*/);
		
		ParseUser.enableAutomaticUser();		
		ParseUser currentUser = ParseUser.getCurrentUser();
		if (ParseAnonymousUtils.isLinked(currentUser)) {
			if (!currentUser.has("RunOnce") || currentUser.getBoolean("RunOnce")) {
				currentUser.put("RunOnce", true);
				currentUser.saveInBackground();
			}
		}
		
		ParseACL defaultACL = new ParseACL();
		defaultACL.setPublicReadAccess(true);
		defaultACL.setWriteAccess(currentUser, true);
		ParseACL.setDefaultACL(defaultACL, true);
	}
}
